<?php
global $current_user;
get_currentuserinfo();
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);

if ($user_role == 'administrator' || $user_role == 'moderator')
{

  echo '<script type="text/javascript" language="javascript" src="'.BUZZR_WP_URL.'/wp-content/pfiles/datatables/jquery.dataTables.min.js"></script>';
  echo '<link rel="stylesheet" type="text/css" media="all" href="'.BUZZR_WP_URL.'/wp-content/pfiles/datatables/demo_page.css" />
  <link rel="stylesheet" type="text/css" media="all" href="'.BUZZR_WP_URL.'/wp-content/pfiles/datatables/demo_table_jui.css" />
  <link rel="stylesheet" type="text/css" media="all" href="'.BUZZR_WP_URL.'/wp-content/pfiles/datatables/jquery-ui-1.7.2.custom.css" />';
    
  function createDataTablesString($data_array, $columns_array){
    $datatable_columns_string = '';
    $datatable_data_string = '';
    
    foreach($columns_array as $columns){
      $datatable_columns_string .= '{ ';
      foreach($columns as $key=>$value){
        //$datatable_columns_string .= '"'.$key.'": "'.$value.'",';
        $datatable_columns_string .= $key.": '".$value."',";
      }
      $datatable_columns_string = substr($datatable_columns_string, 0, -1);
      $datatable_columns_string .= '},';
    }
    $datatable_columns_string = substr($datatable_columns_string, 0, -1);
    
    if(count($data_array))
    {
      $datatable_data_string = '[';
      foreach($data_array as $data)
      {
        $datatable_data_string .= '[';
        foreach($data as $key=>$value)
        {
          $datatable_data_string .= '\'' . $value . '\',';
        }
        $datatable_data_string = substr($datatable_data_string, 0, -1);
        $datatable_data_string .= '],';
      }
      $datatable_data_string = substr($datatable_data_string, 0, -1);
      $datatable_data_string .= ']';
    }
    
    $data['datatable_columns_string'] = $datatable_columns_string;
    $data['datatable_data_string'] = $datatable_data_string;
    
    return $data;
  }
  
  $columns_array[0]['sTitle'] = 'Deal Id';
  $columns_array[0]['sClass'] = 'center';
  $columns_array[0]['sWidth'] = '100px';
  $columns_array[1]['sTitle'] = 'Deal Image';
  $columns_array[1]['sClass'] = 'center';
  $columns_array[1]['sWidth'] = '200px';
  $columns_array[2]['sTitle'] = 'Deal Title';
  $columns_array[2]['sClass'] = 'center';
  $columns_array[2]['sWidth'] = '300px';
  $columns_array[3]['sTitle'] = 'City';
  $columns_array[3]['sClass'] = 'center';
  $columns_array[3]['sWidth'] = '50px';
  $columns_array[4]['sTitle'] = 'Brand';
  $columns_array[4]['sClass'] = 'center';
  $columns_array[4]['sWidth'] = '200px';
  $columns_array[5]['sTitle'] = 'Category';
  $columns_array[5]['sClass'] = 'center';
  $columns_array[5]['sWidth'] = '150px';
  $i = 0;
  
  $deal_pod = new Pod('deal');
  $deal_pod->search = false;
  $deal_pod->page = 1;
  $where = "t.daily = 1 AND t.camp_end >= ".fix_date();
  $deal_pod->findRecords('id DESC', 60, $where);
  
  //printr($deal_pod->getTotalRows(),1);
  
  $category = new Pod('category');
  $category->findRecords('name ASC',-1,"t.is_local = 1 OR t.is_daily = 1");
  $i = 0;
  while($category->fetchRecord()){
    $cat[$i]['id'] = $category->get_field('id');
    $cat[$i]['name'] = pods_sanitize($category->get_field('name'));
    $i++;
  }

  while($deal_pod->fetchRecord()){
      unset($select);
      unset($cat_id);
      unset($deal_id);
      $cat_id = $deal_pod->get_field('camp_cat.id');
      $deal_id = $deal_pod->get_field('id');
      $deal_ids[] = $deal_id;
	  
      $select.= '<select class="category_option" id="category_'.$deal_id.'" name="category"><option value="">-- Select One --</option>';
      foreach($cat as $key => $value){
        if($cat_id == $value['id']){
          $selected = 'selected="yes"';
        }
        $select .= '<option value="'.$deal_id.'~'.$value['id'].'" '.$selected.'>'.$value['name'].'</option>';
        unset($selected);
      }
      $select .= '</select>';
      
      $image = $deal_pod->get_field('image.guid');
      if(empty($image)){
        $image = $deal_pod->get_field('ext_image_url');
      }
      
      $city = $deal_pod->get_field('city.name');
      if(!is_array($city)){
        $cities = '<a href="'.BUZZR_URL.'/dailydeals/'.strtolower($city).'">'.ucwords($city).'</a>';
      }else{
        $cities = array();
        foreach($city as $key=>$value){
          $cities[] = '<a href="'.BUZZR_URL.'/dailydeals/'.strtolower($value).'">'.ucwords($value).'</a>';
        }
        
        $cities = implode(', ',$cities);
      }
      
      $external_link = $deal_pod->get_field('extlink');
      if(!empty($external_link)){
        $ext_link = $deal_id.' '.'<a href="'.$external_link.'">'.$external_link.'</a>';
      }else{
        $ext_link = $deal_id.' '.'<a href="nolink">'.No-Link.'</a>';
      }
      $data_array[$i][] = $ext_link;
       
      if(!empty($image)){
        $data_array[$i][] = '<img src="'.$image.'" height="130" width="180" />';
      }else{
        $data_array[$i][] = '<img src="http://buzzr.in/wp-content/themes/buzzr_960/img/buzzrin-logo.png" height="130" width="180" />';
      }
      
      $data_array[$i][] = pods_sanitize($deal_pod->get_field('name'));
      $data_array[$i][] = $cities;
        //$data_array[$i][] = '<a href="'.$deal_pod->get_field('extlink').'">'.$deal_pod->get_field('extlink').'</a>';
      $data_array[$i][] = '<a href="'.BUZZR_URL.'/brands/'.$deal_pod->get_field('brand.brandurl').'">'.pods_sanitize($deal_pod->get_field('brand.name')).' <img src="'.$deal_pod->get_field('brand.logo.guid').'" height="40" width="100" /></a>';
      
      $start_date = $deal_pod->get_field('camp_start');
      if(isset($start_date) && !empty($start_date)){
      	$start_date = date('d-M-Y',strtotime($start_date));
      }else{
      	$start_date = date('d-M-Y',time());
      }
      
      $data_array[$i][] = $select.' '.$start_date;
                  
      //$data_array[$i][] = $select;
      $i++; 
  }
  //printr(count($data_array));
  //printr($data_array,1);
  
  $data = createDataTablesString($data_array, $columns_array);
  
  $datatable_columns_string = $data['datatable_columns_string'];
  $datatable_data_string = $data['datatable_data_string'];
?>
<script type="text/javascript" charset="utf-8">
  var aDataSet = <?php echo $datatable_data_string ?>;
  jQuery(document).ready(function($){ 
  $(document).ready(function(){
    $('#dynamic').html( '<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"></table>' );
		var oTable = $('#example').dataTable( {
                "aaSorting": [[ 0, "desc" ]],
                "bJQueryUI": true,
                "sPaginationType": "full_numbers",
                "aaData": aDataSet,
			"aoColumns": [
        <?php echo $datatable_columns_string ?>
			]
		});
		
        $(".category_option").live('change',function () {
            var str = $(this).val();
            
            $.ajax({ 
                 type: "post", 
                    url: "<?php echo BUZZR_WP_URL.'/wp-content/plugins/buzzr_moderate_dailydeals/moderate_dailydeals.php'; ?>", data: "id="+str,
                    success: function(msg){
                            $(".result1").empty();  
                            $(".result1").append(msg);
                            alert(msg);
                    }
            });
        });
        
		$('.loading').hide().ajaxStart(function() {
        $('.result1').hide();            
        $(this).show();
    }).ajaxStop(function(){
        $(this).hide();
        $('.result1').show();
    });
    
	});
});
</script>
<div id="container" style="width: 90%; margin: 0 auto; float: none;">
<strong>Assign Daily Deals Category</strong>&nbsp;&nbsp;<span class="loading"><img src="<?php echo BUZZR_WP_URL.'/wp-content/pfiles/images/loader.gif'; ?>" /></span><br /><span class="result1"></span>
  <div id="dynamic"></div>
  <div class="spacer"></div>
</div>
<?php
}
?>